from fileinput import filename

import openpyxl
import pymysql

host = "localhost"
user = "root"
password = "root"
database = "baidu"

wb = openpyxl.load_workbook(filename = r"F:\PythonFile\Define\pythonProject1\mine\ymlg.xlsx")
sheet = wb["员工信息"]


sql = "select * from employees"

db = pymysql.connect(host=host, user=user, passwd=password, db=database, port=3306)
cur = db.cursor()

cur.execute(sql)

data = cur.fetchmany(6)
start_row = 2

# 遍历查询结果，并将数据写入Excel表格
for index, row in enumerate(data):
    # row是一个元组，每个元素对应数据库表的一列
    for col_index, value in enumerate(row, start=1):  # enumerate的start参数设置列索引从1开始
        # 将数据写入Excel表格的对应单元格
        # 注意：这里假设Excel表格的列数与数据库表的列数一致，且顺序相同
        sheet.cell(row=start_row + index, column=col_index, value=value)

cur.close()
db.close()

wb.save(filename=r"F:\PythonFile\Define\pythonProject1\mine\ymlg.xlsx")











